package name.huzhenbo.java.patterns.proxy;

class ProductDBProxy implements Product {
    private Product product;
    private ProductDao productDao;

    public ProductDBProxy(Product product, ProductDao productDao) {
        this.product = product;
        this.productDao = productDao;
    }

    /**
     * 1. The logic is in product implementation.
     * 2. Use DAO to access database.
     */
    public void setWeight(float weight) {
        product.setWeight(weight);
        productDao.save(product);
    }

    public float getWeight() {
        product = (Product) productDao.load(((ProductImplementation) product).getId());
        return product.getWeight();
    }

}
